#!/bin/sh
#(c) Copyright 2012 Barry Kauler
#2012 GPL licence v3 (refer: /usr/share/doc/legal)
#120328 rewrite script.
#120627 use keymaps from kbd pkg if present.
#120629 raspberry pi does not have a hw clock, set date to reasonable value.
#130501 passed param from rc.sysinit.
#130513 modify handling of "ask" passed param.

. /etc/rc.d/PUPSTATE
. /etc/DISTRO_SPECS #has DISTRO_XORG_AUTO
STATUS=0
. /etc/clock
ARCH=`uname -m`
[ "$ARCH" = "x86_64" ] && lsuffix=64 || lsuffix=

[ $pkeys ] && PKEYS=$pkeys #boot parameter

ASKKMAP="yes"; ASKLANG="yes"; ASKCLOCK="no"
[ "$DISTRO_XORG_AUTO" = "yes" ] && ASKKMAP="no" && ASKLANG="no"
[ -f /tmp/bootcnt.txt ] && ASKKMAP="yes" && ASKLANG="yes" #file created in 'init' if pfix=nox.

#130501 passed param from rc.sysinit (rc.country is called 2nd time)... 130513 no, only one call.
if [ $1 ];then
 ASKKMAP="yes"
 ASKLANG="yes"
fi

#120627
KMAPPATH='/lib/keymaps'
[ -d /usr/share/kbd/keymaps/i386 ] && KMAPPATH='/usr/share/kbd/keymaps/i386'
[ -d /usr/share/keymaps/i386 ] && KMAPPATH='/usr/share/keymaps/i386' #debian, gentoo. note, debian files have extension .kmap.gz, gentoo have .map.gz
KEXT=''
[ -f ${KMAPPATH}/qwerty/us.map ] && KEXT='.map'
[ -f ${KMAPPATH}/qwerty/us.map.gz ] && KEXT='.map'
[ -f ${KMAPPATH}/qwerty/us.kmap ] && KEXT='.kmap'
[ -f ${KMAPPATH}/qwerty/us.kmap.gz ] && KEXT='.kmap'

######keyboard section######
askkbdlayout ()
{
 echo -n "us${KEXT}" > /etc/keymap #default.
 echo -n "" > /etc/codepage # "
 FIRSTITEM="us \"(USA)\""
 echo -n "" >/tmp/keybdlist.txt
 case $KMAPPATH in #120627
  /lib/keymaps) KMAPFILES="`find /lib/keymaps -mindepth 1 -maxdepth 1 -name '*.gz' | sort`" ;; #ex: us.gz
  *) KMAPFILES="`find ${KMAPPATH}/[^i]* -name '*.map' -o -name '*.kmap' -o -name '*.map.gz' -o -name '*.kmap.gz' -not -name 'defkeymap*' | sort`" ;;
 esac
 for AFILE in $KMAPFILES
 do
  AKMAP="`basename $AFILE .gz`" #us, fr, br, etc.
  AKMAP="`basename $AKMAP .kmap`" #120627
  AKMAP="`basename $AKMAP .map`" #120627
  case $AKMAP in
   us*) ACNTRY="(USA)";;
   fr*) ACNTRY="(France)";;
   it*) ACNTRY="(Italy)";;
   ba*) ACNTRY="(Bashkir)";;
   be*) ACNTRY="(Belgium)";;
   bg*) ACNTRY="(Bulgaria)";;
   br*) ACNTRY="(Brazil)";;
   by*) ACNTRY="(Byelorussian)";;
   cf*) ACNTRY="(French-Canadian)";;
   croat*) ACNTRY="(Croatia)";;
   cz*) ACNTRY="(Czech)";;
   de) ACNTRY="(Germany)";;
   de_*) ACNTRY="(Germany)";;
   de-*) ACNTRY="(Germany)";;
   dk*) ACNTRY="(Denmark)";;
   es*) ACNTRY="(Spain)";;
   fi*) ACNTRY="(Finland)";;
   hu*) ACNTRY="(Hungary)";;
   ka*) ACNTRY="(Kazakhstan)";;
   ky*) ACNTRY="(Kyrgyzstan)";;
   et*) ACNTRY="(Estonia)";;
   lt*) ACNTRY="(Lithuania)";;
   mk*) ACNTRY="(Macedonia)";;
   sr*) ACNTRY="(Serbia)";;
   tr*) ACNTRY="(Turkey)";;
   tt*) ACNTRY="(Tatar)";;
   sg*) ACNTRY="(Sango)";;
   ua*) ACNTRY="(Ukraine)";;
   gr*) ACNTRY="(Greece)";;
   il*) ACNTRY="(Israel)";;
   is*) ACNTRY="(Iceland)";;
   jp*) ACNTRY="(Japan)";;
   pc*) ACNTRY="(Japanese/English)";;
   la*) ACNTRY="(Latin_America)";;
   nl*) ACNTRY="(Netherlands)";;
   no*) ACNTRY="(Norway)";;
   pl*) ACNTRY="(Poland)";;
   pt*) ACNTRY="(Portugal)";;
   ro*) ACNTRY="(Romania)";;
   ru*) ACNTRY="(Russia)";;
   se*) ACNTRY="(Sweden)";;
   sv*) ACNTRY="(Sweden)";;
   sk*) ACNTRY="(Slovakia)";;
   sl*) ACNTRY="(Slovenia)";;
   uk*) ACNTRY="(UK)";;
   wangbe*) ACNTRY="(Belgium)";;
   azerty) ACNTRY="(Tandon)";;
   dvorak-r) ACNTRY="(Right_single-handed)";;
   dvorak-l) ACNTRY="(Left_single-handed)";;
   *)  ACNTRY="-";;
  esac
  echo -e "$AKMAP \"${ACNTRY}\"" >> /tmp/keybdlist.txt
 done
 KEYLIST="${FIRSTITEM} `sort /tmp/keybdlist.txt | tr '\n' ' '`"
 echo '#!/bin/sh' >/tmp/keydlg
 echo 'dialog --aspect 10 --no-cancel --menu "Select the keyboard layout:\n(UP/DOWN arrows then ENTER key)" 0 0 0 \' >>/tmp/keydlg #' geany fix
 echo "$KEYLIST 2> /tmp/kbdextlayout.txt" >>/tmp/keydlg
 echo 'exit $?' >>/tmp/keydlg
 chmod 755 /tmp/keydlg
 /tmp/keydlg >/dev/console
 [ $? != 0 ] && return 1
 clear > /dev/console 2>&1 #clear screen
 KMAP=`cat /tmp/kbdextlayout.txt`
 case $KMAP in #note, same/similar code in /usr/sbin/input-wizard, init, quicksetup.
  de*|be*|br*|dk*|es*|fi*|fr*|it*|no*|se*|sv*|pt*) echo -n "850" > /etc/codepage ;;
  cz*|hu*|pl*|ro*|sk*|croat*|slovene*)  echo -n "852" > /etc/codepage  ;;
 esac
 echo -n "${KMAP}${KEXT}" > /etc/keymap
 return 0
}

if [ "$ASKKMAP" = "no" ];then
 #'quicksetup' is run 1st time X starts.
 [ ! -f /etc/keymap ] && echo -n "us${KEXT}" > /etc/keymap
 [ ! -f /etc/codepage ] && echo -n "" > /etc/codepage
else
 [ ! -e /etc/keymap ] && askkbdlayout #creates /etc/keymap, /etc/codepage.
fi

if [ -s /etc/codepage ];then
 CODEPAGE="`cat /etc/codepage`"
 case $CODEPAGE in
  850) modprobe nls_cp850 ;;
  852) modprobe nls_cp852 ; modprobe nls_iso8859-2 ;;
 esac
fi

#note, "keymap-set --xopt" is run in xorgwizard and xorgwizard-automatic,
# "keymap-set --load" is run in quicksetup.
if [ -s /etc/keymap ];then
 NEED2LOADKMAP="yes"
 [ -d /initrd -a "$PKEYS" ] && NEED2LOADKMAP="no" #already done in initrd.
 if [ "$NEED2LOADKMAP" = "yes" ];then
  KMAP="`cat /etc/keymap`"
  case $KMAPPATH in #120627
   /lib/keymaps) gzip -dcf /lib/keymaps/$KMAP.gz | loadkmap ;;
   *) loadkeys $KMAP ;; #note i think don't need to specify the extension (but it is in KMAP).
  esac
 fi
fi
######end keyboard section######

######locale section######
LANG="`grep '^LANG=' /etc/profile | cut -f 2 -d '='`"
CLANG="`echo "$LANG" | sed -e 's/\.UTF-8/\.utf8/'`" #fix, locale -a always prints .utf8

if [ "$ASKLANG" = "no" ];then
 #'quicksetup' is run 1st time X starts.
 #need this default locale for X to start first time...
 # added $lsuffix 140113
 [ ! -d /usr/lib${lsuffix}/locale/en_US ] && localedef -f ISO-8859-1 -i en_US --no-archive en_US >/dev/null
 [ ! -d /usr/lib${lsuffix}/locale/${CLANG} ] && /usr/sbin/chooselocale cli quiet #ask no questions, will run localedef.
else
 #localedef creates locale files in /usr/lib/locale...
 if [ "`locale -a | grep "$CLANG"`" = "" ];then
  if [ "$LANG" = "en_US" ];then
   /usr/sbin/chooselocale cli > /dev/console
  else
   /usr/sbin/chooselocale cli quiet #ask no questions, will run localedef.
  fi
  /sbin/load_consolefont #note, also called from rc.sysinit, xwin.
  ASKCLOCK="yes"
 fi
fi
######end locale section######

######clock section######
#120629 raspberry pi does not have a hw clock, set date to reasonable value...
HWDATE="`hwclock --show 2>/dev/null`" #ex: "Fri 29 Jun 2012 07:45:28 AM WST  -0.725833 seconds"
if [ "$HWDATE" = "" ];then
 if [ -f /var/local/shutdown_date_saved ];then #see /etc/rc.d/rc.shutdown
  date -s "`cat /var/local/shutdown_date_saved`"
 else
  #either of these formats can set the date: "29 JUN 2012 10:00:00" "2012-06-28 16:20:08"
  HWDATE="`stat --format=%z /etc/DISTRO_SPECS | cut -f 1 -d '.' | cut -f 1,2 -d ' '`" #creation date of build. ex: "2012-06-28 16:20:08"
  date -s "$HWDATE"
  [ ! -e /etc/localtime ] && ln -s /usr/share/zoneinfo/Australia/Perth /etc/localtime
 fi
else
 if [ "$ASKCLOCK" = "yes" ];then
  /usr/sbin/timezone-set cli > /dev/console
  #choose hardware clock set to local or UTC...
  dialog --title "Set hardware-clock type" --yes-label "Local" --no-label "UTC" --yesno "Is the hardware clock on the computer set to the local time, or to UTC? Note, most PCs with Windows installed have the hardware clock set to the local time, so if in doubt choose that..." 0 0 > /dev/console
  if [ $? -eq 1 ];then #UTC
   HWCLOCKTIME='utc'
   hwPATTERN="s/^HWCLOCKTIME=[^#]*/HWCLOCKTIME='utc'/"
   sed -i "$hwPATTERN" /etc/clock #note, it is default 'localtime'.
  fi
 fi
 [ ! -e /etc/localtime ] && ln -s /usr/share/zoneinfo/Australia/Perth /etc/localtime
 #Ref: http://www.gnu.org/s/libc/manual/html_node/TZ-Variable.html
 TZ=$(readlink /etc/localtime|sed -e 's,/usr/share/zoneinfo/,,' -e 's,Etc/,,') #shinobar
 export TZ
 HWCLOCKTIME="localtime"
 [ -s /etc/clock ] && grep -qw '^[^#]*utc' /etc/clock && HWCLOCKTIME="utc" #shinobar
 #need to set Linux system time/date, from hardware clock...
 #hwclock --hctosys --localtime
 hwclock --hctosys --${HWCLOCKTIME}
 #...--hctosys reads cmos clock to system.
 #...--localtime means that cmos clock is set to local-time.
fi
######end clock section######

exit $STATUS
###END###
